<?php

// ====================================================
// FileName:con02_baselogic.inc.php
// Summary: 数据表基本操作
// Author:fp
// CreateTime: 2016-06-20
// LastModifed:2021-05-21
// ====================================================
class baselogic
{
    // 数据访问层
    var $dbh;
    // 表的名称
    var $tblName;
    // 字段名数组
    var $fieldList;
    // 表主键数组
    var $keyList;

    /**
     * 函数 baselogic
     * 功能 构造函数
     * 参数 $dbh mysql对象
     */
    function baselogic($dbh)
    {
        $this->dbh = $dbh;
    }

    /**
     * 函数 db_getdata
     * 功能 数据库检索
     * 参数 $sql 检索SQL文
     */
    function db_getdata($sql)
    {
        // SQL执行
        $stmt = $this->dbh->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * 函数 db_getitem
     * 功能 根据数据表名返回数据表内项目
     * 参数 无
     */
    function db_getitem()
    {
        $value = "";
        foreach ($this->fieldList as $v) {
            $value .= $v . ", ";
        }
        $value = rtrim($value, ", ");
        return $value;
    }

    /**
     * 函数 selcount
     * 功能 数据是否存在判断
     * 参数 $postList 提交的数据列表
     */
    function selcount($postList, $delflg = '')
    {
        $sql = "SELECT count(*) as con FROM {$this->tblName} WHERE ";
        
        $where = "";
        foreach ($this->keyList as $v) {
            if (isset($postList[$v])) {
                if (! get_magic_quotes_gpc())
                    $where .= $v . " = '" . addslashes($postList[$v]) . "' and ";
                else
                    $where .= $v . " = '" . $postList[$v] . "' and ";
            }
        }
        
        if (! empty($delflg)) {
            $where .= " delflg='$delflg'";
        } else {
            $where = rtrim($where, "and ");
        }
        $sql .= $where;
        
        // print_r($sql);
        $res = $this->db_getdata($sql);
        return $res[0]['con'];
    }

    /**
     * 函数 insert
     * 功能 数据表添加
     * 参数 $userid 用户ID
     * 参数 $postList 提交的数据列表
     */
    function insert($userid, $postList)
    {
        $sql = "INSERT INTO {$this->tblName}";
        $columns = "(";
        $value = "VALUES(";
        
        foreach ($this->fieldList as $v) {
            if (array_key_exists($v, $postList)) {
                $columns .= $v . ",";
                if ($postList[$v] === null) {
                    $value .= "null,";
                } else {
                    if (! get_magic_quotes_gpc()) {
                        $value .= "'" . addslashes($postList[$v]) . "',";
                    } else {
                        $value .= "'" . $postList[$v] . "',";
                    }
                }
            }
        }
        $columns .= " createymd, createid, updateymd, updateid, delflg)";
        $sql .= $columns . $value . "now(), '$userid', now(), '$userid', '0')";
        
        // echo $sql;exit();
        if ($this->dbh->query($sql)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 函数 update
     * 功能 数据表修改
     * 参数 $userid 用户ID
     * 参数 $postList 提交的数据列表
     */
    function update($userid, $postList)
    {
        $sql = "UPDATE {$this->tblName} SET ";
        $value = "";
        
        foreach ($this->fieldList as $v) {
            if (array_key_exists($v, $postList)) {
                if ($postList[$v] === null) {
                    $value .= $v . " = null,";
                } else {
                    if (! get_magic_quotes_gpc()) {
                        $value .= $v . " = '" . addslashes($postList[$v]) . "',";
                    } else {
                        $value .= $v . " = '" . $postList[$v] . "',";
                    }
                }
            }
        }
        $value .= "delflg=0, updateymd = now(), updateid = '" . $userid . "'";
        
        $where = "";
        foreach ($this->keyList as $v) {
            if (isset($postList[$v])) {
                if (! get_magic_quotes_gpc())
                    $where .= $v . " = '" . addslashes($postList[$v]) . "' and ";
                else
                    $where .= $v . " = '" . $postList[$v] . "' and ";
            }
        }
        $where = rtrim($where, "and ");
        $sql .= $value . " WHERE " . $where;
        
        // echo $sql;exit();
        if ($this->dbh->query($sql)) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 函数 delete
     * 功能 数据表删除
     * 参数 $userid 用户ID
     * 参数 $postList 提交的数据列表
     * 参数 $flag true:物理;false:逻辑;
     */
    function delete($userid, $postList, $flag = false)
    {
        if ($flag) {
            $sql = "DELETE FROM {$this->tblName}";
        } else {
            $sql = "UPDATE {$this->tblName} SET delflg=1, updateymd = now(), updateid = '$userid'";
        }
        
        $where = "";
        foreach ($this->fieldList as $v) {
            if (isset($postList[$v])) {
                if (! get_magic_quotes_gpc())
                    $where .= $v . " = '" . addslashes($postList[$v]) . "' and ";
                else
                    $where .= $v . " = '" . $postList[$v] . "' and ";
            }
        }
        $where = rtrim($where, "and ");
        $sql .= " WHERE " . $where;
        
        // echo $sql;exit();
        if ($this->dbh->query($sql)) {
            return 1;
        } else {
            return 0;
        }
    }
}
?>